/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package inquiry;

import item.Item;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.sql.Date;
import javax.persistence.*;
import user.Customer;
import user.Employee;
import myJPA.Model;

/**
 *
 * @author Nacer Khalil
 */
@Entity
@Table(name="item_inquiry")
@NamedQuery(name="getInquiries",query="select l from ItemInquiry l")
public class ItemInquiry extends Inquiry{
    @Transient
    private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
    
    
    @ManyToOne
    @JoinColumn(name = "cus_id")
    private Customer customer;
    
    @ManyToOne
    @JoinColumn(name = "item_id")
    private Item item;
    
    
    
    
    
    public ItemInquiry() {
        super();
    }

    public ItemInquiry(String message,Date date, Customer customer, Item item) {
        super(message, date);
        this.customer = customer;
        this.item = item;
    }

    /**
     * @return the customer
     */
    public Customer getCustomer() {
        return customer;
    }

    /**
     * @return the item
     */
    public Item getItem() {
        return item;
    }
    
    
    
    public String respondInquiry(String inquiryId, String response , Employee employ)
    {
        String result=null;
        EntityManager manager = Model.getEntityManager();
        
        ItemInquiry inquiry= Model.getEntityManager().find(ItemInquiry.class, inquiryId);
        result = result +"\n" +inquiry.getItem().getTitle()+ "\n"+ inquiry.getItem().getType()+"\n"+inquiry.getItem().getAvailableCopies();
        inquiry.setResponse(response);
        inquiry.setEmployee(employ);
        inquiry.update();
        return result;
        
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final ItemInquiry other = (ItemInquiry) obj;
        if (this.customer != other.customer && (this.customer == null || !this.customer.equals(other.customer))) {
            return false;
        }
        if (this.item != other.item && (this.item == null || !this.item.equals(other.item))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        return hash;
    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        changeSupport.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        changeSupport.removePropertyChangeListener(listener);
    }
    
    
    
    
  
}
